//
//  LrcTools.swift
//  LocalMusicPlayer
//
//  Created by Darren on 16/9/28.
//  Copyright © 2016年 darren. All rights reserved.
//

import UIKit

typealias dataArrayClosure = (Array<Any>) -> Void

class LrcTools: NSObject {
    
    var dataArray:Array<Any> = []
    var dataArrClosure:dataArrayClosure?

    func setupLrcArrayAndLtrTimerArr(model:musicModel)->Array<Any> {
        // 歌词
        let urlLyc = URL(string: model.LRC!)
        
        let lrcStr = try? String(contentsOf: urlLyc!, encoding: String.Encoding.utf8)
        if lrcStr==nil {
            return [[],[]]
        }

        let lrcArray = lrcStr?.components(separatedBy: "\n")
        
        // 取出不含时间的文字
        var mutArr:[String] = []
        var mutArr2:[String] = []
        
        var totalArr:[String] = []
        
        for lrc in lrcArray! {
            let titleArr = lrc.components(separatedBy: "]")
            
            for i in 0..<titleArr.count-1{
                let lineLrc = titleArr[i] + "]" + titleArr.last!
                if lineLrc.hasPrefix("[offset:") {
                    continue
                }
                totalArr.append(lineLrc)
            }
        }
        
        // 对totalArr进行排序
        totalArr.sort { (re1, re2) -> Bool in
            var strTimer1:Float = 0
            var strTimer2:Float = 0
            if re1.contains(".")==true&&re2.contains(".")==true{
                let range = NSMakeRange(1, 8)
                let str1 = (re1 as NSString).substring(with: range)
                strTimer1 = self.timeStringWithString(str1)
                let str2 = (re2 as NSString).substring(with: range)
                strTimer2 = self.timeStringWithString(str2)
            }
            return strTimer1<strTimer2
        }
        
        for lrc2 in totalArr {
            let titleArr = lrc2.components(separatedBy: "]")
            let timerStr = titleArr.first!
            
            mutArr2.append(timerStr)
            var titleStr = titleArr.last!
            
            if titleStr=="\r"&&timerStr.hasPrefix("[ti:")==false&&timerStr.hasPrefix("[ar:")==false&&timerStr.hasPrefix("[al:")==false&&timerStr.hasPrefix("[by:")==false{
                mutArr.append("*雪峰音乐*")
            }
            if titleStr.characters.count<2 {
                continue
            }
            
            let titleStr2 = (titleStr as NSString).substring(to: titleStr.characters.count-1)
            mutArr.append(titleStr2)
        }
        
        // 取出时间
        var mutArr3:[String] = []
        for i in 0..<mutArr2.count {
            
            let lrctimer = mutArr2[i]
            
            if lrctimer.hasPrefix("[ti:")||lrctimer.hasPrefix("[ar:")||lrctimer.hasPrefix("[al:")||lrctimer.hasPrefix("[by:")||lrctimer.hasPrefix("[offset") {
                continue
            }
            let lyr = lrctimer.substring(from: lrctimer.characters.index(lrctimer.startIndex, offsetBy: 1))
            mutArr3.append(lyr)
        }
        
        self.dataArray.append(mutArr)
        self.dataArray.append(mutArr3)
        return self.dataArray
    }

    func timeStringWithString(_ timeString:String)->Float{
        let min:Double = Double(timeString.components(separatedBy: ":")[0])!
        let range = NSMakeRange(3, 2)
        let second:Double = Double((timeString as NSString).substring(with: range))!
        let haomiao:Double = Double(timeString.components(separatedBy: ".")[1])!
        let timer1 = min*60
        let timer2 = haomiao*0.01
        return (Float)(timer1+timer2+second)
    }

}
